// pages/courseMap/courseMap.js
// 引入SDK核心类
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');

// 实例化API核心类
var qqmapsdk = new QQMapWX({
  key: 'KTEBZ-RCD62-DNFU5-CA6XK-6EGJ2-R2FOF' // 必填
});
var app = getApp()
var mymap = '';
var lat = '';
var long = '';
Page({
  data: {
    markers: [
      {
        id: 0,
        alpha: 0,
        width: 5,
        height: 5,
        latitude: 31.099994,
        longitude: 121.324520,
        iconPath: '../../img/triangle.png',
        callout: {
          content: 'xx区\nxx路xx号\n\n暑假3日小程序课堂',
          fontSize: 10,
          color: "#000",
          bgColor: "#fff",
          borderWidth: 1,
          // borderColor: "#007788",
          padding: 5,
          display: "ALWAYS",
          borderRadius:10,
        },
        // },
        //   {
        //     id: 1,
        //     width: 5,
        //     height: 5,
        //     latitude: 30.999994,
        //     longitude: 121.324520,
        //     iconPath: '../../img/triangle.png',
        //     callout: {
        //       content: '气泡名称1111\n换行试试',
        //       fontSize: 10,
        //       color:"#ffffff",
        //       bgColor: "#007788",
        //       borderWidth: 1,
        //       borderColor: "#007788",
        //       padding: 4,
        //       display: "ALWAYS",
        //     },
      }
    ],
    // controls: [{
    //   id: 1,
    //   iconPath: '../../img/test.png',
    //   position: {
    //     left: 90,
    //     top: 235,
    //     width: 50,
    //     height: 50
    //   },
    //   clickable: true
    // }],
    // 根据地点判断的经纬度数组
    points: [
      // {
      //   latitude: 31.099994,
      //   longitude: 121.324520,
      // },
      // {
      //   latitude: 30.999994,
      //   longitude: 121.324520,
      // }
    ],
    distance: [],   //距离
    myC: null,     //是对象，单条数据，根据点击气泡刷新数据
  },

  onLoad: function (option) {
    
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        console.log(res);
        that.setData({
          hasLocation: true,
          location: {
            longitude: res.longitude,
            latitude: res.latitude
          }
        }, () => {

          // wx.chooseLocation({
          //   success:function(res){
          //     console.log(res,"------");

          //   }
          //   });
          qqmapsdk.geocoder({
            address: '上海市徐汇区裕德路126号',
            success: function (res) {
              console.log(res, "------");
              // wx.openLocation({
              //   name: `${res.result.address_components.province}-${res.result.address_components.district}`,
              //   address: `${res.result.address_components.province}${res.result.address_components.district}${res.result.title}`,
              //   latitude: res.result.location.lat,
              //   longitude: res.result.location.lng
              // })
              // lat = res.result.location.lat
              // long = res.result.location.lng
              // var m = []
              // var op = res.result.location
              // m.push(op)
              // that.setData({
              //   points:m
              // })
              // that.formSubmit(op)
              // console.log(that.data.points)
            },
            fail: function (res) {
              console.log(res);
            },
            complete: function (res) {
              //console.log(res,"------77777");
            }
          })
        })
      }
    }) 
  },
  jump:function(){
    wx.redirectTo(
      {
        url: '../courseDetail/courseDetail',
      }
    )
  },

  formSubmit1(e) {
    var _this = this;
    //调用地址解析接口
    qqmapsdk.geocoder({
      //获取表单传入地址
      address: e.detail.value.geocoder, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
      success: function (res) {//成功后的回调
        console.log(res);
        var res = res.result;
        var latitude = res.location.lat;
        var longitude = res.location.lng;
        //根据地址解析在地图上标记解析地址位置
        console.log(_this.data.markers.length)
        _this.data.markers.push({
          id: _this.data.markers.length,
          title: res.title,
          latitude: latitude,
          longitude: longitude,
          // iconPath: './resources/placeholder.png',//图标路径
          width: 20,
          height: 20,
          callout: { //可根据需求是否展示经纬度
            content: 'zuqiu测试',
            fontSize: 10,
            color: "#ffffff",
            bgColor: "#007788",
            borderWidth: 1,
            borderColor: "#007788",
            padding: 4,
            display: "ALWAYS",
          }
        })
        console.log(_this.data.markers)
      },
      fail: function (error) {
        console.error(error);
      },
      complete: function (res) {
        // console.log(res);
      }
    })

    this.formSubmit()
  },
  formSubmit(date) {

    var _this = this;
    //调用距离计算接口
    qqmapsdk.calculateDistance({
      //mode: 'driving',//可选值：'driving'（驾车）、'walking'（步行），不填默认：'walking',可不填
      //from参数不填默认当前地址
      //获取表单提交的经纬度并设置from和to参数（示例为string格式）
      // from: _this.data.my, //若起点有数据则采用起点坐标，若为空默认当前地址
      to: date, //终点坐标
      success: function (res) {//成功后的回调
        // console.log(res);
        var res = res.result;
        var dis = [];
        for (var i = 0; i < res.elements.length; i++) {
          var dis1 = parseInt(res.elements[i].distance);
          if (dis1 >= 1000) {
            var dis1 = dis1 / 1000
          }
          dis.push(dis1); //将返回数据存入dis数组，
        }
        _this.setData({ //设置并更新distance数据
          distance: dis
        });
        // console.log(_this.data.distance)
      },
      fail: function (error) {
        console.error(error);
      },
      complete: function (res) {
        console.log(res);
      }
    });
  }
})
